﻿using System;
using System.IO;
using System.Text;

namespace Terraria.Localization
{
	// Token: 0x02000326 RID: 806
	public class NetworkText
	{
		// Token: 0x06001E41 RID: 7745 RVA: 0x00015787 File Offset: 0x00013987
		private NetworkText(string text, NetworkText.Mode mode)
		{
			this._text = text;
			this._mode = mode;
		}

		// Token: 0x06001E42 RID: 7746 RVA: 0x00441798 File Offset: 0x0043F998
		private static NetworkText[] ConvertSubstitutionsToNetworkText(object[] substitutions)
		{
			NetworkText[] array = new NetworkText[substitutions.Length];
			for (int i = 0; i < substitutions.Length; i++)
			{
				NetworkText networkText = substitutions[i] as NetworkText;
				if (networkText == null)
				{
					networkText = NetworkText.FromLiteral(substitutions[i].ToString());
				}
				array[i] = networkText;
			}
			return array;
		}

		// Token: 0x06001E43 RID: 7747 RVA: 0x0001579D File Offset: 0x0001399D
		public static NetworkText FromFormattable(string text, params object[] substitutions)
		{
			return new NetworkText(text, NetworkText.Mode.Formattable)
			{
				_substitutions = NetworkText.ConvertSubstitutionsToNetworkText(substitutions)
			};
		}

		// Token: 0x06001E44 RID: 7748 RVA: 0x000157B2 File Offset: 0x000139B2
		public static NetworkText FromLiteral(string text)
		{
			return new NetworkText(text, NetworkText.Mode.Literal);
		}

		// Token: 0x06001E45 RID: 7749 RVA: 0x000157BB File Offset: 0x000139BB
		public static NetworkText FromKey(string key, params object[] substitutions)
		{
			return new NetworkText(key, NetworkText.Mode.LocalizationKey)
			{
				_substitutions = NetworkText.ConvertSubstitutionsToNetworkText(substitutions)
			};
		}

		// Token: 0x06001E46 RID: 7750 RVA: 0x004417DC File Offset: 0x0043F9DC
		public int GetMaxSerializedSize()
		{
			int num = 0;
			num++;
			num += 4 + Encoding.UTF8.GetByteCount(this._text);
			if (this._mode != NetworkText.Mode.Literal)
			{
				num++;
				for (int i = 0; i < this._substitutions.Length; i++)
				{
					num += this._substitutions[i].GetMaxSerializedSize();
				}
			}
			return num;
		}

		// Token: 0x06001E47 RID: 7751 RVA: 0x000157D0 File Offset: 0x000139D0
		public void Serialize(BinaryWriter writer)
		{
			writer.Write((byte)this._mode);
			writer.Write(this._text);
			this.SerializeSubstitutionList(writer);
		}

		// Token: 0x06001E48 RID: 7752 RVA: 0x00441834 File Offset: 0x0043FA34
		private void SerializeSubstitutionList(BinaryWriter writer)
		{
			if (this._mode == NetworkText.Mode.Literal)
			{
				return;
			}
			writer.Write((byte)this._substitutions.Length);
			for (int i = 0; i < (this._substitutions.Length & 255); i++)
			{
				this._substitutions[i].Serialize(writer);
			}
		}

		// Token: 0x06001E49 RID: 7753 RVA: 0x00441880 File Offset: 0x0043FA80
		public static NetworkText Deserialize(BinaryReader reader)
		{
			NetworkText.Mode mode = (NetworkText.Mode)reader.ReadByte();
			string text = reader.ReadString();
			NetworkText networkText = new NetworkText(text, mode);
			networkText.DeserializeSubstitutionList(reader);
			return networkText;
		}

		// Token: 0x06001E4A RID: 7754 RVA: 0x004418AC File Offset: 0x0043FAAC
		public static NetworkText DeserializeLiteral(BinaryReader reader)
		{
			NetworkText.Mode mode = (NetworkText.Mode)reader.ReadByte();
			string text = reader.ReadString();
			NetworkText networkText = new NetworkText(text, mode);
			networkText.DeserializeSubstitutionList(reader);
			if (mode != NetworkText.Mode.Literal)
			{
				networkText.SetToEmptyLiteral();
			}
			return networkText;
		}

		// Token: 0x06001E4B RID: 7755 RVA: 0x004418E0 File Offset: 0x0043FAE0
		private void DeserializeSubstitutionList(BinaryReader reader)
		{
			if (this._mode == NetworkText.Mode.Literal)
			{
				return;
			}
			this._substitutions = new NetworkText[(int)reader.ReadByte()];
			for (int i = 0; i < this._substitutions.Length; i++)
			{
				this._substitutions[i] = NetworkText.Deserialize(reader);
			}
		}

		// Token: 0x06001E4C RID: 7756 RVA: 0x000157F1 File Offset: 0x000139F1
		private void SetToEmptyLiteral()
		{
			this._mode = NetworkText.Mode.Literal;
			this._text = string.Empty;
			this._substitutions = null;
		}

		// Token: 0x06001E4D RID: 7757 RVA: 0x00441928 File Offset: 0x0043FB28
		public override string ToString()
		{
			try
			{
				switch (this._mode)
				{
				case NetworkText.Mode.Literal:
					return this._text;
				case NetworkText.Mode.Formattable:
				{
					string text = this._text;
					object[] substitutions = this._substitutions;
					return string.Format(text, substitutions);
				}
				case NetworkText.Mode.LocalizationKey:
				{
					string text2 = this._text;
					object[] substitutions = this._substitutions;
					return Language.GetTextValue(text2, substitutions);
				}
				default:
					return this._text;
				}
			}
			catch (Exception ex)
			{
				string str = "NetworkText.ToString() threw an exception.\n";
				str += this.ToDebugInfoString("");
				str += "\n";
				str = str + "Exception: " + ex.ToString();
				this.SetToEmptyLiteral();
			}
			return this._text;
		}

		// Token: 0x06001E4E RID: 7758 RVA: 0x00441A04 File Offset: 0x0043FC04
		private string ToDebugInfoString(string linePrefix = "")
		{
			string text = string.Format("{0}Mode: {1}\n{0}Text: {2}\n", linePrefix, this._mode, this._text);
			if (this._mode == NetworkText.Mode.LocalizationKey)
			{
				text += string.Format("{0}Localized Text: {1}\n", linePrefix, Language.GetTextValue(this._text));
			}
			if (this._mode != NetworkText.Mode.Literal)
			{
				for (int i = 0; i < this._substitutions.Length; i++)
				{
					text += string.Format("{0}Substitution {1}:\n", linePrefix, i);
					text += this._substitutions[i].ToDebugInfoString(linePrefix + "\t");
				}
			}
			return text;
		}

		// Token: 0x04001951 RID: 6481
		public static readonly NetworkText Empty = NetworkText.FromLiteral("");

		// Token: 0x04001952 RID: 6482
		private NetworkText[] _substitutions;

		// Token: 0x04001953 RID: 6483
		private string _text;

		// Token: 0x04001954 RID: 6484
		private NetworkText.Mode _mode;

		// Token: 0x02000327 RID: 807
		private enum Mode : byte
		{
			// Token: 0x04001956 RID: 6486
			Literal,
			// Token: 0x04001957 RID: 6487
			Formattable,
			// Token: 0x04001958 RID: 6488
			LocalizationKey
		}
	}
}
